/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package SourceCode;

import java.awt.FileDialog;
import java.awt.Frame;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

/**
 *
 * @author ssaad
 */
public class SerializationGenerator {

    /**
     * @param args the command line arguments
     */
        public static  void Generate() {
        // TODO code application logic here
        ArrayList<Copy> copies = new ArrayList<Copy>();
        ArrayList<Book> books = new ArrayList<Book>();
        ArrayList<Magazine> magazines = new ArrayList<Magazine>();

        //members
        ArrayList<Member> members = new ArrayList<Member>();
        Member member1 = new Member(1, "Sayed Saad", "Fairfield", "641-472-7000");
        Member member2 = new Member(2, "zheng Yang", "Fairfield", "641-472-8000");
        Member member3 = new Member(3, "Ankur Goyal", "Fairfield", "641-472-9000");
        Member member4 = new Member(4, "Goyal Ankur", "Fairfield", "641-472-6000");
        Member member5 = new Member(5, "Yang zheng", "Fairfield", "641-472-5000");
        Member member6 = new Member(6, "Saad Saad", "Fairfield", "641-472-4000");
        members.add(member1);
        members.add(member2);
        members.add(member3);
        members.add(member4);
        members.add(member5);
        members.add(member6);



        Book book1 = new Book("karmen", 21, "19826", "sheksper");
        Book book2 = new Book("Programming", 21, "23654", "Dietl");
        Book book3 = new Book("Java Script", 21, "125677", "For Dummy Company");
        Book book4 = new Book("NLP", 21, "90987", "steven");
        Book book5 = new Book("Open Mind", 21, "34652", "smith");
        Book book6 = new Book("VB.net", 21, "90453", "Vector");
        books.add(book1);
        books.add(book2);
        books.add(book3);
        books.add(book4);
        books.add(book5);
        books.add(book6);

        Magazine mag1= new Magazine("Design", 7, "8724");
        Magazine mag2= new Magazine("Furniture", 3, "9253");
        Magazine mag3= new Magazine("Computer", 7, "9128");
        magazines.add(mag1);
        magazines.add(mag2);
        magazines.add(mag3);


        Copy copy1_1 = new Copy(100, book1);
        Copy copy1_2 = new Copy(101, book1);
        Copy copy1_3 = new Copy(102, book1);
        Copy copy2_1 = new Copy(200, book2);
        Copy copy2_2 = new Copy(201, book2);
        Copy copy2_3 = new Copy(202, book2);
        Copy copy3_1 = new Copy(300, book3);
        Copy copy3_2 = new Copy(301, book3);
        Copy copy3_3 = new Copy(302, book3);
        Copy copy4_1 = new Copy(400, book4);
        Copy copy$_2 = new Copy(401, book4);
        Copy copy4_3 = new Copy(402, book4);
        Copy copy4_4 = new Copy(403, book4);
        Copy copy5_1 = new Copy(500, book5);
        Copy copy5_2 = new Copy(501, book5);
        Copy copy6_1 = new Copy(600, book6);

        Copy copy6_2 = new Copy(601, book6);
        Copy copyM1_1 = new Copy(700, mag1);
        Copy copym1_2 = new Copy(701, mag1);
        Copy copym1_3 = new Copy(702, mag1);

        Copy copym2_1 = new Copy(800, mag2);
        Copy copym2_2 = new Copy(801, mag2);
        Copy copym3_1 = new Copy(900, mag3);
        Copy copym3_2 = new Copy(901, mag3);
        copies.add(copy1_1);
        copies.add(copy1_2);
        copies.add(copy1_3);
        copies.add(copy2_1);
        copies.add(copy2_2);
        copies.add(copy2_3);
        copies.add(copy3_1);
        copies.add(copy3_2);
        copies.add(copy3_3);
        copies.add(copy4_1);
        copies.add(copy$_2);
        copies.add(copy4_3);
        copies.add(copy4_4);
        copies.add(copy5_1);
        copies.add(copy5_2);
        copies.add(copy6_1);
        copies.add(copy6_2);
        copies.add(copyM1_1);
        copies.add(copym1_2);
        copies.add(copym1_3);

        copies.add(copym2_1);
        copies.add(copym2_2);
        copies.add(copym3_1);
        copies.add(copym3_2);


        /*76290
        Copy java1 = new Copy("core1-110", true) ;
        java1.setCopyForItem(ditel);
        member1.borrow(java1);

        Copy Design2 = new Copy("Design-201", true) ;
        Design2.setCopyForItem(ditel);
        member1.borrow(Design2);

        Copy Develop3 = new Copy("Develop-235", true) ;
        Develop3.setCopyForItem(karmen);
        member2.borrow(Develop3);
        //reservation

        member1.reserveI(karmen);
        //print all details

        //return Copy 1
        member1.returncopy(java1);
        members.add(member1);
        members.add(member2);

       for(Member m : members)
       {


        for(Loan lone : m.loans)
        {
            System.out.println("Loans by " + lone.getMember().tostring());
            System.out.println("Loans  " + lone.getCopy().tostring());
        }

        for(Reservation r : m.reservations)
        {
            System.out.println("Reserved  by " + r.getMember().tostring());
            System.out.println("Loans  " + r.getItem().tostring());
        }

       }
        */
        //ssssssssssssssssssssssssssssssssssssssssssssssssssss
       try
       {
        FileDialog fd = new FileDialog(new Frame(),"Select file to save ", FileDialog.SAVE);
        fd.show();
        String filePath = new  String( fd.getDirectory() + fd.getFile());

        FileOutputStream fos = new FileOutputStream(filePath, false);

        ObjectOutputStream oos = new ObjectOutputStream(fos);
           //oos.writeObject(members);
            //oos.writeObject(books);
            //oos.writeObject(magazines);
            oos.writeObject(copies);

            
        System.out.println("Finished success " + filePath );
        oos.close();
        fos.close();
//         //////////////////////
/*        FileInputStream fis = new FileInputStream(filePath);
         ObjectInputStream ois = new ObjectInputStream(fis);

         ArrayList<Copy> m =  new ArrayList<Copy>();
         m.addAll((ArrayList<Copy>)  ois.readObject());
         /*ArrayList<Copy> copies1 =  new ArrayList<Copy>();
         copies1.addAll((ArrayList<Copy>)  ois.readObject());
          *
         
        System.out.println("Finished success " + filePath );
         for(Copy c : m)
         {
             System.out.println("Copy id " + c.getCopyid() );

         }


ois.close();
fis.close();
   */
//System.out.println("length  " + m.size());
       }
       catch(Exception ex)
       {
            System.out.println("Not Valid");
       }

    }

public static Object getSerialization(String filePath)
{
    Object obj = new Object();
     try
       {
//        FileDialog fd = new FileDialog(new Frame(),"Select file to save ", FileDialog.LOAD);
//        fd.show();
//        String filePath = new  String( fd.getDirectory() + fd.getFile());

        FileInputStream fis = new FileInputStream(filePath);
        ObjectInputStream ois = new ObjectInputStream(fis);
        
        obj = ois.readObject();

        System.out.println("Finished success " );
        ois.close();
        fis.close();
        return obj;
       }
       catch(Exception ex)
       {
            System.out.println("Not Valid");
            return null;
       }

}

public static void setSerialization(Object obj)
{

     try
       {
        FileDialog fd = new FileDialog(new Frame(),"Select file to save ", FileDialog.SAVE);
        fd.show();
        String filePath = new  String( fd.getDirectory() + fd.getFile());
        FileOutputStream fos = new FileOutputStream(filePath, true);

        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(obj);

         System.out.println("Finished success");
        oos.flush();
        fos.close();
        System.out.println("Finished success " + filePath );
       }
       catch(Exception ex)
       {
            System.out.println("Not Valid");

       }

}

}
